package com.push.web.utils;

import com.google.common.base.Splitter;
import com.push.common.utils.DateUtils;
import com.push.common.utils.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    //验证结果
    public static void main(String[] args) throws FileNotFoundException {
    // 同时替换多个文字
        String str = "2016年<b>台风</b>“莫兰蒂”侵袭时，因梅溪排洪不畅，导致洪濑镇区大面积内涝，积水最深处近3米，洪濑镇4.5万人受灾，造成直接经济损失高达6.9亿元；2023年<b>台风</b>“杜苏芮”&nbsp;...";
    }

    public static String getUrl(String url, String name) {
        url += "&";
        String pattern = "(\\?|&){1}#{0,1}" + name + "=[a-zA-Z0-9]*(&{1})";
        Pattern r = Pattern.compile(pattern);
        Matcher matcher = r.matcher(url);
        if (matcher.find()) {
            return matcher.group(0).split("=")[1].replace("&", "");
        } else {
            return null;
        }
    }

    public static String getParam(String url, String name) {
        String params = url.substring(url.indexOf("?") + 1, url.length());
        Map<String, String> split = Splitter.on("&").withKeyValueSeparator("=").split(params);
        return split.get(name);
    }

    private static String addColor(String html){
        if (StringUtils.isEmpty(html)){
            return html;
        }
        Document doc = Jsoup.parse(html, "UTF-8");
        Elements imgList = doc.body().getElementsByTag("b");
        for(Element element : imgList){
            element.attr("style","color:#F00");
        }
        String newStr = doc.body().toString();
        return newStr.substring(6,newStr.length()-7).replaceAll("(\\r\\n|\\n|\\\\n)", "").trim();
    }




    public static Date parseDate(String inputDate) {
        Date outputDate = null;
        String[] possibleDateFormats =
                {
                        "yyyy-MM-dd",
                        "yyyyMMdd",
                        "yyyy/MM/dd",
                        "yyyy年MM月dd日",
                        "yyyy MM dd"
                };
        try {
            outputDate = DateUtils.parseDate(inputDate, possibleDateFormats);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return outputDate;
    }
}
